﻿Imports System.ServiceModel.DomainServices.Client

Partial Public Class FullDeCaixaReserves
    Inherits ChildWindow

    Private _Reserva As Web.Reserva
    Public ReadOnly Property Reserva As Web.Reserva
        Get
            Return _Reserva
        End Get
    End Property

    Private _Mode As EnumMode
    Public ReadOnly Property Mode As EnumMode
        Get
            Return _Mode
        End Get
    End Property

    Public Enum EnumMode
        ModeAlta
        ModeModificacio
    End Enum

#Region "Crear i Destruir"
    Public Sub New(pMode As EnumMode, pReserva As Web.Reserva)
        MyBase.New()

        InitializeComponent()
        _Mode = pMode
        _Reserva = pReserva

        Me.cboTipusPagament.ItemsSource = OpcionsComboTipusPagaments.GetOpcions
        Me.cboPais.ItemsSource = OpcionsComboTipusPaisos.GetOpcions
        Me.cboSexe.ItemsSource = OpcionsComboTipusSexes.GetOpcions
        Me.cboTipusDocument.ItemsSource = OpcionsComboTipusDocuments.GetOpcions
    End Sub

    Private Sub Child_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)
    End Sub

    Private Sub FullDeCaixa_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If RiaContext.Current.Authentication.User.Identity.IsAuthenticated = True Then

            If Mode = EnumMode.ModeAlta Then
                Me.Title = StringLibrary.AltaReserva
                _Reserva.IdTipusPagament = EnumTipusPagament.Cash
                _Reserva.Import = 0

            ElseIf Mode = EnumMode.ModeModificacio Then
                Me.Title = StringLibrary.ModificarReserva

            End If

            Me.gridDetall.DataContext = _Reserva

        End If
    End Sub
#End Region

#Region "Guardar i Cancelar"
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EliminarObjectErrors()

        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            If ValidarDades() And Validacio.Errors.Count = 0 Then
                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Function ValidarDades() As Boolean
        Dim Resultat As Boolean = True

        Return Resultat
    End Function

    Private Sub DomainContext_SubmittedChanges(ByVal s As SubmitOperation)
        Activity.IsActive = False

        If s.HasError Then
            For Each E As Entity In s.EntitiesInError
                For Each VError As ComponentModel.DataAnnotations.ValidationResult In E.ValidationErrors
                    Me.Validacio.Errors.Add(New ValidationSummaryItem(VError.ErrorMessage))
                Next
            Next
            s.MarkErrorAsHandled()            
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            LullabyFullDeCaixa.Contexte.DomainContext.RejectChanges()
        End If

        Me.DialogResult = False
    End Sub

    Public Sub EliminarObjectErrors()
        Dim i As Long

        i = 0
        While i < Validacio.Errors.Count
            If Validacio.Errors(i).ItemType = ValidationSummaryItemType.ObjectError Then
                Validacio.Errors.RemoveAt(i)
            Else
                i = i + 1
            End If
        End While
    End Sub
#End Region


End Class
